package com.hhit.collaborative_learning.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.List;

import com.hhit.collaborative_learning.dao.BBS_ClassDao;
import com.hhit.collaborative_learning.entity.BBSClass;

public class BBS_ClassDaoImpl implements BBS_ClassDao {

	PreparedStatement ps = null;
	ResultSet rs = null;

	@Override
	public ResultSet queryBBS_Class(Connection con, int start, int end,
			String findwhate, String findby, String order, String sort) {
		// TODO Auto-generated method stub
		String sql = "SELECT * FROM `collaborative_learning`.`bbs_class` ";
		if (findwhate != null && findby != null) {
			sql += " where " + findwhate + " " + findby + " ";
		}
		if (sort != null) {
			sql += " order by " + sort + " ";
			if (order != null) {
				sql += " " + order;
			}
		}
		if (end != 0 && start < end) {
			sql += " limit " + start + "," + end;
		} 
		PreparedStatement prs;
		try {
			prs = con.prepareStatement(sql);
			System.out.println("请求：" + sql);
			return prs.executeQuery();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			System.out.println("非法请求：" + sql);
			return null;
		}
	}
	
	public int getCount(Connection conn,String findwhate,String findby) {
		int num = 0;
		String sql = "select count(*) from `collaborative_learning`.`bbs_class`";
		if (findwhate != null && findby != null) {
			sql += " where " + findwhate + " " + findby + " ";
		}
		try {
			PreparedStatement prs = conn.prepareStatement(sql);
			ResultSet res = prs.executeQuery();
			if (res.next()) {
				num = res.getInt(1);
			}
		} catch (SQLException ex) {
			ex.printStackTrace();
		} 
		return num;
	}

	@Override
	public int delete(Connection con, String find) {
		// TODO Auto-generated method stub
		String sql = "DELETE FROM `collaborative_learning`.`bbs_class` WHERE "+find+" ";
		try {
			PreparedStatement prs = con.prepareStatement(sql);
			return prs.executeUpdate();
		} catch (SQLException ex) {
			ex.printStackTrace();
			System.out.println("非法sql语句  "+sql);
			return 0;
		} 
	}

	@Override
	public int insert(Connection con, String ctitle, String cIntro,
			String crule, String icon_select,String sp_id) {
		String sql="INSERT INTO `collaborative_learning`.`bbs_class` (`Name`,`Intro`,`Rule`,`ImageUrl`,`Createdon`,`specialID`)VALUES(?,?,?,?,?,?)";
		try {
			PreparedStatement prs = con.prepareStatement(sql);
			prs.setString(1, ctitle);
			prs.setString(2, cIntro);
			prs.setString(3, crule);
			prs.setString(4, icon_select);
			prs.setTimestamp(5, new Timestamp(System.currentTimeMillis()));
			prs.setString(6, sp_id);
			return prs.executeUpdate();
		} catch (SQLException ex) {
			ex.printStackTrace();
			return 0;
		} 
	}
}
